#region Using

using System;
using System.Xml;
using System.IO;
using System.Globalization;
using System.Collections.Generic;
using System.Collections.Specialized;
using BlogEngine.Core;
using BlogEngine.Core.Providers.EloqueraProvider.Db;
using Eloquera.Client;
using System.Collections;

#endregion

namespace BlogEngine.Core.Providers {
    /// <summary>
    /// A storage provider for BlogEngine that uses XML files.
    /// <remarks>
    /// To build another provider, you can just copy and modify
    /// this one. Then add it to the web.config's BlogEngine section.
    /// </remarks>
    /// </summary>
    public partial class EloqueraBlogProvider : BlogProvider {

        /// <summary>
        /// Retrieves a Page from the data store.
        /// </summary>
        public override Page SelectPage(Guid id) {
            Parameters param = EqDb.CreateParameters();
            param["id"] = id;
            EqPage eqPage = (EqPage)EqDb.ExecuteScalar("select EqPage where Id = @id", param);
            if (eqPage != null) {
                Page page = new Page() {
                    Title = eqPage.Title,
                    Description = eqPage.Description,
                    Content = eqPage.Content,
                    Keywords = eqPage.Keywords,
                    Slug = eqPage.Slug,
                    Parent = eqPage.Parent,
                    IsFrontPage = eqPage.IsFrontPage,
                    ShowInList = eqPage.ShowInList,
                    IsPublished = eqPage.IsPublished,
                    DateCreated = eqPage.DateCreated,
                    DateModified = eqPage.DateModified
                };
                return page;
            } else {
                return null;
            }
        }

        /// <summary>
        /// Inserts a new Page to the data store.
        /// </summary>
        public override void InsertPage(Page page) {
            Parameters param = EqDb.CreateParameters();
            param["id"] = page.Id;
            EqPage eqPage = (EqPage)EqDb.ExecuteScalar("select EqPage where Id = @id", param);
            if (eqPage == null) {
                eqPage = new EqPage();
            }
            eqPage.Title = page.Title;
            eqPage.Description = page.Description;
            eqPage.Content = page.Content;
            eqPage.Keywords = page.Keywords;
            eqPage.Slug = page.Slug;
            eqPage.Parent = page.Parent;
            eqPage.IsFrontPage = page.IsFrontPage;
            eqPage.ShowInList = page.ShowInList;
            eqPage.IsPublished = page.IsPublished;
            eqPage.DateCreated = page.DateCreated;
            eqPage.DateModified = page.DateModified;

            if (Page.Pages.IndexOf(page) < 0) {
                Page.Pages.Add(page);
            }
            EqDb.Store(eqPage);
        }

        /// <summary>
        /// Updates a Page.
        /// </summary>
        public override void UpdatePage(Page page) {
            InsertPage(page);
        }

        /// <summary>
        /// Deletes a page from the data store.
        /// </summary>
        public override void DeletePage(Page page) {
            Parameters param = EqDb.CreateParameters();
            param["id"] = page.Id;
            EqPage eqPage = (EqPage)EqDb.ExecuteScalar("select EqPage where Id = @id", param);
            if (eqPage != null) {
                EqDb.Delete(eqPage);
            }
            if (Page.Pages.Contains(page))
                Page.Pages.Remove(page);
        }

        /// <summary>
        /// Retrieves all pages from the data store
        /// </summary>
        /// <returns>List of Pages</returns>
        public override List<Page> FillPages() {
            List<Page> pages = new List<Page>();
            IEnumerable eqPages = EqDb.ExecuteQuery("select EqPage");
            foreach (var obj in eqPages) {
                EqPage eqPage = (EqPage)obj;
                Page page = new Page() {
                    Title = eqPage.Title,
                    Description = eqPage.Description,
                    Content = eqPage.Content,
                    Keywords = eqPage.Keywords,
                    Slug = eqPage.Slug,
                    Parent = eqPage.Parent,
                    IsFrontPage = eqPage.IsFrontPage,
                    ShowInList = eqPage.ShowInList,
                    IsPublished = eqPage.IsPublished,
                    DateCreated = eqPage.DateCreated,
                    DateModified = eqPage.DateModified
                };
                pages.Add(page);
            }
            return pages;
        }
    }
}
